package poker;

public class Deck {

	private Card[] cards;

	public Deck() {
		cards = new Card[52];
		int i = 0;
		for(int s  = 0; s < 4 ; s++){//suits iteration
			for(int n = 1; n <= 13 ; n++){//card number iteration
				cards[i] = new Card(n, s);
				i++;
			}
		}
	}
	/**
	 * Copy constructor using shallow copy
	 * @param other
	 */
	public Deck(Deck other) {
		Card[] copyCards = new Card[other.cards.length];
		for(int i = 0; i < other.cards.length; i++){
			copyCards[i] = other.cards[i];
		}
		cards = copyCards;
	}
	
	
	/**
	 * A method that finds a card present at a specific position in the array
	 * @param position
	 * @return card at that position
	 */
	public Card getCardAt(int position) {
		return cards[position];

	}
	
	/**
	 * A method that finds the size of the array of Cards.
	 * @return size of the array
	 */

	public int getNumCards() {
		return cards.length;
	}
	
	/**
	 * A method that rearranges the cards in the deck by dividing them into two 
	 * "packets"-top half and bottom half.The new array of cards will consist 
	 * of:  the first card from the top packet, followed by the first card from
	 * the bottom packet, and so on
	 */

	/* hint: even and odd */
	public void shuffle() {
		Card[] shuffle = new Card[getNumCards()];
		if(getNumCards() % 2 == 0){//deck even
			int i =0;
			//before shuffle deck iterating from start of deck
			for(int f = 0; f < getNumCards(); f=f+2){
				shuffle[f] = getCardAt(i);
				i++;            
			}

			//before shuffle deck iterating from middle of deck
			int j = getNumCards()/2;
			for(int g = 1; g < getNumCards(); g= g + 2){
				shuffle[g] = getCardAt(j);
				j++;
			}
		}

		if(getNumCards() % 2 != 0){//deck odd
			int k =0;
			//before shuffle deck iterating from start of deck
			for(int h = 0; h < getNumCards(); h = h +2){
				shuffle[h] = getCardAt(k);
				k++;
			}

			//before shuffle deck iterating from middle+1 of the deck
			int l = (int)getNumCards()/2 + 1;
			for(int i = 1; i < getNumCards(); i = i + 2){
				shuffle[i] = getCardAt(l);
				l++;
			}
		}
		cards = shuffle;
	}
	
	/**
	 *  This method divides the deck into two sub packets:  The part above the
	 *  specified position, and the part that is at the specified position or 
	 *  below. The two sub packets are reversed 
	 * @param position
	 */

	public void cut(int position){
		Card[] cut = new Card[getNumCards()];

		for(int i=0; i<getNumCards();i++) {
			if(i<getNumCards()-position) {
				cut[i] = cards[ i+ position ];
			} else {
				cut[i] = cards[i -(cards.length- position)];
			}
		}
		cards = cut;
	}
	
	/**
	 * This method removes the specified number of cards from the top of
	 * the deck and return them as an array.
	 * @param numCards
	 * @return temp
	 */

	public Card[] deal(int numCards) {
		Card[] smaller = new Card[getNumCards() - numCards];
		Card[] temp = new Card[numCards];

		//putting cards taken out into an array called temp
		for(int i = 0; i < numCards; i++){
			temp[i] = getCardAt(i);
		}
		
       int k = 0;
		//putting cards left into an array called smaller, resizing cards array
		for(int j = numCards; j < getNumCards(); j++){
			smaller[k] = cards[j];
         k++;
		}
		cards = smaller;
		return temp;
	}
}




